<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (! defined ( 'IN_TINPHP' )) {
	exit ( 'Access Denied' );
}

require_once ('indexApp.php');

/**
 * 收货地址管理
 * 
 * @author ZhangXiLian 2014-09-12
 */
class AddressController extends IndexAppController {
	private $addressLogic;
	public function __construct() {
		parent::__construct ();
		$this->addressLogic = new AddressLogic ();
	}
	/**
	 * 收货地址管理列表
	 */
	public function actionDefault() {
		$cond = "STATUS=1 AND USER_ID=" . $this->user ['ID'] . " ORDER BY IS_DEFAULT DESC";
		$address = $this->addressLogic->getAddressList ( "*", $cond );
		T::$tpl->set ( "addressList", $address ["data"] );
	}
	/**
	 * 保存会员资料修改
	 */
	public function actionAjaxSave() {
		$res = array ();
		$res ['code'] = "error";
		
		$userId = $this->user ["ID"];
		$cols = "*";
		$cond_count = "STATE = 1 AND USER_ID = " . $userId . " ORDER BY IS_DEFAULT DESC";
		$count = $this->addressLogic->getAddressItem ( "count(*) as total", $cond_count );
		if (! $_POST ["address_id"] && $count ["total"] >= 20) {
			$res ['msg'] = '您已经添加了20个收货地址，不能再添加了！';
			echo $this->getResources ( $res );
			return;
		}
		$realname = RequestUtil::getPost('realname');
		$geo_code = RequestUtil::getPost('geo_code');
		$mobile = RequestUtil::getPost('mobile');
		if($realname=="" || $geo_code=="" || $mobile==""){
			$res ['msg'] = '请确认带*号的是否填写';
			echo $this->getResources ( $res );
			return;
		}
		
		$address_map = Tin::createDataMap ();
		$address_map->addEntry ( 'USER_ID', $userId, DB::INT );
		$address_map->addEntry ( 'REALNAME', $realname, DB::VARCHAR );
		// $fav_map->addEntry('URL', urldecode($_REQUEST["url"]), DB::INT);
		$address_map->addEntry ( 'GEO_CODE', $geo_code, DB::INT );
		$address_map->addEntry ( 'ADDRESS', RequestUtil::getPost("address"), DB::VARCHAR );
		$address_map->addEntry ( 'ZIP_CODE', RequestUtil::getPost("zip_code"), DB::VARCHAR );
		$address_map->addEntry ( 'TEL', RequestUtil::getPost("tel"), DB::VARCHAR );
		$address_map->addEntry ( 'MOBILE', $mobile, DB::VARCHAR );
		$address_map->addEntry ( 'STATUS', 1, DB::INT );
		
		if ($_REQUEST ["is_default"]) {
			$defalutcond = "IS_DEFAULT=1 AND USER_ID=" . $userId;
			$defalutRes = $this->addressLogic->getAddressItem ( "*", $defalutcond );
			if ($defalutRes) {
				$commLogic = new CommonLogic ( 'WEB_UADDRESS' );
				$dataArr = array ();
				$dataArr ['IS_DEFAULT'] = 0;
				$commLogic->updateFieldVal ( $dataArr, "ID=" . $defalutRes ["ID"] );
			}
			$address_map->addEntry ( 'IS_DEFAULT', '1', DB::VARCHAR );
		} else {
			$address_map->addEntry ( 'IS_DEFAULT', '0', DB::VARCHAR );
		}
		
		if (! $_POST ["address_id"]) {
			$address_map->addEntry ( 'CREATE_DATE', "now()", DB::DATETIME );
			$address_map->addEntry ( 'STATUS', 1, DB::INT );
			$resDB = $this->addressLogic->addAddressItem ( $address_map );
		} else {
			$cond = "USER_ID =" . $userId . " AND ID =" . $_POST ["address_id"];
			$resDB = $this->addressLogic->updateAddressByCond ( $address_map, $cond );
		}
		
		if ($resDB) {
			$res ['code'] = "success";
			$res ['msg'] = "保存收货地址成功";			
		} else {
			$res ['code'] = "error";
			$res ['msg'] = "保存失败，请返回重试！";			
		}
		echo $this->getResources ( $res );
	}
	/** 获取单条记录 */
	public function actionAjaxGetAddress(){
		$address_id = $_REQUEST["address_id"];
		$userid=$this->user["ID"];
		$res = array ();
		$res ['code'] = "error";
		if($address_id){
			$cond = "USER_ID = " . $userid." AND ID = " . $address_id;
			$address = $this->addressLogic->getAddressItem ( "*", $cond );
			if($address){
				$res ['code'] = "success";
				$res ['msg'] = "";
				$res ['data'] = $address;
			}else{
				$res ['msg'] = "无效记录";
			}
		}else{
			$res ['msg'] = "未知参数";
		}
		echo $this->getResources ( $res );
	}
	/** 删除记录 */
	public function actionAjaxDelAddress(){
		$address_id = $_REQUEST["address_id"];
		$res = array ();
		$res ['code'] = "error";
		if($address_id){
			$cond = "ID=" . $address_id . " AND USER_ID=".$this->user["ID"];
			$commLogic = new CommonLogic ( 'WEB_UADDRESS' );
			$dataArr = array ();
			$dataArr ['STATUS'] = 2;
			$dbRes = $commLogic->updateFieldVal ( $dataArr, $cond );
			if($dbRes){
				$res ['code'] = "success";
				$res ['msg'] = "删除成功";
			}else{
				$res ['msg'] = "删除失败";
			}
		}else{
			$res ['msg'] = "未知参数";
		}
		echo $this->getResources ( $res );
	}
	
} // End CLass
?>
